#pragma once

#include <cv.h>
#include <highgui.h>
#include <string>
#include <iostream>

class DebugImage
{
public:
	DebugImage(IplImage* i, const std::string &window) :img(i), win(window) { 
		#ifdef _DEBUG
			std::cout << "Hit a key to show \"" << win << "\"" << std::endl; 
			
			cvWaitKey(); cvNamedWindow(win.c_str()); cvShowImage(win.c_str(), img);
			cvSetMouseCallback(window.c_str(), &DebugImage::DIMouseCallback, img);
		#endif
		}
	~DebugImage(void) { 
		#ifdef _DEBUG
			std::cout << "Hit a key to update \"" << win << "\"" << std::endl; cvWaitKey(); cvShowImage(win.c_str(), img); 
		#endif
		} ;
private:

	static void DIMouseCallback(int event, int x, int y, int flags, void* param)
	{
		if (event == CV_EVENT_LBUTTONDOWN)
		{
			IplImage *img = (IplImage *)param;
			uchar r = CV_IMAGE_ELEM(img, uchar, y, x * 3 + 0);
			uchar g = CV_IMAGE_ELEM(img, uchar, y, x * 3 + 1);
			uchar b = CV_IMAGE_ELEM(img, uchar, y, x * 3 + 2);
			printf("\n(%d, %d) : %u %u %u", x, y, r, g, b);
		}
	}

	IplImage* img;
	std::string win;
};